<?php 
/**
 * @Author: Kendy
 * @Date:   2019-10-22 10:34:15
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2019-11-13 08:46:41
 */
namespace app\api\model; 
use think\Model;
use think\Db; 
class Guide extends Model
{
 /**
   * [GetGuideList 根据条件获取导游列表]
   * @Author   Kendy
   * @DateTime 2019-11-11T10:20:40+0800
   * @param    [type]                   $where     [条件]
   * @param    [type]                   $page      [当前页面]
   * @param    [type]                   $limit     [每页显示条数，默认为配置里面的]
   */
  public function GetGuideList($where,$page,$limit='',$field='')
  { 
        $orderBy = input('order_by','1','trim');    
        $limit = ($limit==='')?config('page_size'):$limit;
        $field= ($field==='')?"guide_head,guide_id,name,guide_tag,guide_rank,guide_money,sales_volume,is_recommend":$field;
        $orderFile = array('1'=>'sales_volume','2'=>'guide_money');
    		$orderSort = array('0'=>'asc','1'=>'desc');
    		if($orderBy==1) $order = 1;        //order_by为1，销售量倒序
    		elseif($orderBy==2) $order = 0;    //order_by为2，价格为正序
    		else $order = 1;                   //order_by为3，则为好评率倒序 
    		if(3>$orderBy && $orderBy>0)
    			$order = array($orderFile[$orderBy]=>$orderSort[$order]);
        elseif($orderBy==4)
          $where['is_cars'] = 1;
        else
          $order = 1;
        $guideList = Db::name('guide') 
           ->where($where) 
           ->field($field)
           ->order($order) 
           ->limit(($page - 1) * $limit,$limit)
           ->select();   
         if(count($guideList)>0)
         {
            foreach ($guideList as $k => $v) {
               $guideList[$k]['appraise'] = model('AppraiseModel')->getAppRate(4,$v['guide_id']);
               $guideList[$k]['guide_tag'] = explode(',', $v['guide_tag']);
               if($v['guide_rank']==1) $guideList[$k]['guide_rank'] = '金牌导游';
               elseif($v['guide_rank']==2)  $v['guide_rank'] = '优质导游';
               else $v['guide_rank'] = '优质导游';
            }
            //按照好评排序，
             if($orderBy==3)
            {  
                $last_names = array_column($guideList,'appraise');
                array_multisort($last_names,SORT_DESC,$guideList); 
            } 
            return $guideList;
         }  
       return $guideList;
  }

  /**
   * [GetGuideInfoById 通过导游id获取导游信息]
   * @Author   kendy
   * @DateTime 2019-11-12T09:47:42+0800
   * @param    [type]                   $guideId [导游id]
   */
  public function GetGuideInfoById($guideId)
  {   
    $resultData = Db::name('guide') 
    ->field('guide_id,is_cars,guide_head,guide_rank,guide_tag,guide_money,guide_content') 
    ->where(['is_del'=>0,'is_pass'=>1,'guide_id'=>$guideId])
    ->find(); 
    if(empty($resultData))
      return apiReturn('400','导游信息为空',0);
    $images = Db::name('guide_img') 
    ->field('img_path')
    ->where('guide_id',$guideId)
    ->select();
    if(count((array)$images)>0)
    {
      foreach ($images as $k => $v) {
       $resultData['guide_img'][] = $v['img_path'];
      }
    }
     $resultData['guide_tag'] = explode(',', $resultData['guide_tag']);
     if($resultData['guide_rank']==1) $resultData['guide_rank'] = '金牌导游';
      elseif($resultData['guide_rank']==2)  $resultData['guide_rank'] = '优质导游';
      else $resultData['guide_rank'] = '优质导游';
      ///获取该景点的最近一条好评
     $resultData['app'] = model('AppraiseModel')->getAppList(4,(int)$resultData['guide_id'],1,1,1); 
      //评价个数  
     $resultData['app'][0]['count'] =model('AppraiseModel')->getAppCountByItemId(4,(int)$resultData['guide_id']); 
      //相关攻略  
     $resultData['related_method'] = model('method')->method_list(['is_select'=>1],1,4);  
    return $resultData; 
  }

    /**
  * [getGuideReserveInfo 获取预约导游页面]
  * @Author   kendy
  * @DateTime 2019-11-12T11:22:34+0800
  * @param    [type]                   $guideId [description]
  * @return   [type]                            [description]
  */
  public function getGuideReserveInfo($guideId)
  { 
    $resultData = Db::name('guide') 
    ->field('guide_id,is_cars,guide_money') 
     ->where(['is_del'=>0,'is_pass'=>1,'guide_id'=>$guideId])
    ->find(); 
    if(!empty($resultData)){
        $carInfo = Db::name('guide_car') 
        ->field('car_money,car_model,car_code,car_image') 
        ->where(['is_display'=>0,'status'=>0,'guide_id'=>$guideId])
        ->find(); 
        return array_merge((array)$resultData,(array)$carInfo);
     }
     return $resultData;
  }

}